﻿using RabbitMQ.Client;
using System;
using System.Text;
using System.Threading;

namespace RabbitMQDemo.Basic.Producer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //简单队列（点对点）
            //SendMessage();
            //工作队列
            //SendMessage1();
            ////发布订阅
            //SendMessage2();
            ////路由模式
            //SendMessage3();
            ////主题模式
            //SendMessage4();

            SendMessage5();



        }

        public static void SendMessage5()
        {
            using (var conn = RabbitMQHelper.GetConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    var exchangeName = "topics_exchange1";
                    channel.ExchangeDeclare(exchange: exchangeName, type: "topic");
                    while (true)
                    {
                        Console.WriteLine("消息RoutingKey:");
                        var routingKey = Console.ReadLine();

                        Console.WriteLine("消息内容(exit退出):");
                        var message = Console.ReadLine();
                        if (message.Trim().ToLower() == "exit")
                        {
                            break;
                        }

                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: exchangeName, routingKey: routingKey, basicProperties: null, body: body);
                        Console.WriteLine("消息内容发送完毕:" + message);
                    }
                }
            }
        }
        public static void SendMessage4()
        {
            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare("topic_exchange", "topic");

                    string queueName1 = "topic_queue1";
                    channel.QueueDeclare(queueName1, false, false, false, null);
                    string queueName2 = "topic_queue2";
                    channel.QueueDeclare(queueName2, false, false, false, null);
                    string queueName3 = "topic_queue3";
                    channel.QueueDeclare(queueName3, false, false, false, null);

                    channel.QueueBind(queue: queueName1, exchange: "topic_exchange", routingKey: "user.data.#");
                    channel.QueueBind(queue: queueName2, exchange: "topic_exchange", routingKey: "user.data.delete");
                    channel.QueueBind(queue: queueName3, exchange: "topic_exchange", routingKey: "user.data.update");

                    // routingKey:user.data.abc
                    for (int i = 0; i < 10; i++)
                    {
                        string message = $"Topic abc Message {i + 1}";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("topic_exchange", "user.data.abc", null, body);
                        Console.WriteLine($"Sended Topic abc Message {i}");
                    }
                    // routingKey:user.data.abc
                    for (int i = 0; i < 10; i++)
                    {
                        string message = $"Topic delete Message {i + 1}";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("topic_exchange", "user.data.delete", null, body);
                        Console.WriteLine($"Sended Topic delete Message {i}");
                    }
                    // routingKey:user.data.update
                    for (int i = 0; i < 10; i++)
                    {
                        string message = $"Topic update Message {i + 1}";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("topic_exchange", "user.data.update", null, body);
                        Console.WriteLine($"Sended Topic update Message {i}");
                    }
                }
            }
        }

        public static void SendMessage3()
        {
            using (var connection = RabbitMQHelper.GetConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare("direct_exchange", "direct", durable: false, autoDelete: false, arguments: null);
                for (int i = 0; i < 3; i++)
                {
                    string queueName = $"direct_queue_{i + 1}";
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(queue: queueName, exchange: "direct_exchange", routingKey: $"direct_key_{i + 1}", arguments: null);
                }

                for (int i = 0; i < 3; i++)
                {
                    var tempRoutingKey = $"direct_key_{i + 1}";
                    for (int j = 0; j < 10; j++)
                    {
                        string message = $"Direct Message {j} and RoutingKey：direct_key_{i + 1}";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "direct_exchange", routingKey: tempRoutingKey, false, null, body);
                        Console.WriteLine($"Sended Message：{j} ****** RoutingKey：direct_key_{i + 1}");
                    }
                }
            }
        }

        public static void SendMessage2()
        {
            using (var connection = RabbitMQHelper.GetConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare("fanout_exchange", "fanout", durable: false, autoDelete: false, arguments: null);
                for (int i = 0; i < 3; i++)
                {
                    string queueName = $"fanout_queue_{i + 1}";
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(queue: queueName, exchange: "fanout_exchange", routingKey: "", arguments: null);
                }
                for (int i = 0; i < 10; i++)
                {
                    string message = $"Fanout Message {i}";
                    var body = Encoding.UTF8.GetBytes(message);
                    channel.BasicPublish(exchange: "fanout_exchange", routingKey: "", false, null, body);
                    Console.WriteLine($"Sended Fanout Message：{i}");
                }
            }
        }

        public static void SendMessage1()
        {
            string queueName = "Worker_Queue";

            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    for (int i = 0; i < 20; i++)
                    {
                        string message = $"Worker Queue Message {i}";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "", routingKey: queueName, null, body);
                        Console.WriteLine($"Sended Message {i}");
                    }
                }
            }
        }


        public static void SendMessage()
        {
            string queueName = "simplest";

            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    while (true)
                    {
                        string message = "Simplest Message";
                        var body = Encoding.UTF8.GetBytes(message);

                        channel.BasicPublish(exchange: "", routingKey: queueName, null, body);
                        Thread.Sleep(1000);
                        Console.WriteLine("Sended Simplest message");
                    }
                }
            }
        }

        public static void SendMessageFist()
        {
            using (var conn = RabbitMQHelper.GetConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    var queueName = "helloworld";
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);

                    while (true)
                    {
                        Console.WriteLine("消息内容(exit退出):");
                        var message = Console.ReadLine();
                        if (message.Trim().ToLower() == "exit")
                        {
                            break;
                        }

                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
                        Console.WriteLine("消息内容发送完毕:" + message);
                    }
                }
            }
        }
    }
}
